package cn.ligoo.part.web.springmvc.platform;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ligoo.part.domain.UserInfo;
import cn.ligoo.part.service.impl.FacadeImpl;
import cn.ligoo.part.web.Constants;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

@Controller
@RequestMapping("/platform/user-info")
public class UserInfoController {
	private final Logger logger = LoggerFactory.getLogger(UserInfoController.class);

	@Resource
	private MessageSource message;

	@Resource
	private FacadeImpl facade;

	public UserInfoController() {
		logger.debug("...constructor");
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping(value = "/search", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String search(HttpServletRequest request, @RequestParam int page, @RequestParam int rows, UserInfo t) {
		logger.debug("...search");

		logger.debug("...page:{}, rows:{}", page, rows);
		logger.debug("...userInfo:{}", t.toString());

		int total = facade.getUserInfoService().getUserInfoCount(t);

		t.getRow().setOffset((page - 1) * rows);
		t.getRow().setLimit(rows);
		t.setIs_admin(Constants.BYTE_0); // 管理员除外
		List<UserInfo> userInfoList = facade.getUserInfoService().getUserInfoList(t);

		SerializeConfig sc = new SerializeConfig();
		sc.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(userInfoList, sc), total);

		logger.debug("...json:{}", json);
		return json;
	}

	@RequestMapping(value = "/delete", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String delete(@RequestParam String[] ids) {
		logger.debug("...delete ids:{}", ids);
		UserInfo t = new UserInfo();
		t.getMap().put("__ids", ids);
		t.getMap().put("__isLogicalDelete", "true");

		int count = 0;
		try {
			count = facade.getUserInfoService().removeUserInfo(t);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, count);
	}

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String save(UserInfo userInfo) {
		logger.debug("...save");
		logger.debug("userInfo:{}", userInfo.toString());
		try {
			if (null == userInfo.getId()) {
				userInfo.setIs_virtual(Constants.BYTE_0);
				userInfo.setCreate_time(new Date());
				facade.getUserInfoService().createUserInfo(userInfo);
			} else {
				facade.getUserInfoService().modifyUserInfo(userInfo);
			}
		} catch (DuplicateKeyException dke) {
			String msg = message.getMessage("errors.dae.DuplicateKeyException", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, null);
	}

	@RequestMapping(value = "/edit/{id}", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String edit(@PathVariable Integer id) {
		logger.debug("...edit");
		UserInfo t = new UserInfo();
		t.setId(id);
		UserInfo userInfo = facade.getUserInfoService().getUserInfo(t);
		logger.debug("...userInfo:{}", userInfo);

		SerializeConfig sc = new SerializeConfig();
		sc.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
		return JSON.toJSONString(userInfo, sc);
	}
}
